package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.custom_widget.slider.RangeBar;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.contract.bean.CustomerBean;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/9/22.
 */

public class CustomerAdapter extends BaseAdapter {
    private List<CustomerBean> list = new ArrayList<>();
    private Context context;
    private int height;
    private int current1, current2, current3, current4, current5, current6;


    public CustomerAdapter(List<CustomerBean> list, Context context, int height) {
        this.list = list;
        this.context = context;
        this.height = height;
    }

    @Override
    public int getCount() {
        if (list.size() > 6) {
            return 6;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.customer_item_layout, null);
            holder = new ViewHolder();
            holder.customer_iv = (ImageView) convertView.findViewById(R.id.custom_item_iv);
            holder.customer_tv = (TextView) convertView.findViewById(R.id.custom_item_tv);
            holder.customerRangeBar = (RangeBar) convertView.findViewById(R.id.custom_rangebar);
            holder.customerRangeBar.setTag(position);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (list != null && list.size() > 0) {
            final CustomerBean customerBean = list.get(position);
            if (customerBean != null) {
                if (customerBean.getName() != null) {
                    holder.customer_tv.setText(customerBean.getName());
                }
                holder.customerRangeBar.setProgress((int) customerBean.getPercent());
                BaseApplication.getApplicationInstance().displayImg(customerBean.getUrl(), holder.customer_iv);
                holder.customerRangeBar.setOnUIRangeBarChangeListener(new RangeBar.OnUIRangeBarChangeListener() {
                    @Override
                    public void onProgressChanged(int i) {
                        LogUtils.showLog("滑动了-------" + i );
                        switch (position) {
                            case 0:
                                calculatedProgressValue(i, 0 , customerBean, holder);

                                break;
                            case 1:
                                calculatedProgressValue(i,1 , customerBean, holder);
                                break;
                            case 2:
                                calculatedProgressValue(i,2 , customerBean, holder);

                                break;
                            case 3:
                                calculatedProgressValue(i,3, customerBean, holder);

                                break;
                            case 4:
                                calculatedProgressValue(i, 4 ,customerBean, holder);

                                break;
                            case 5:
                                calculatedProgressValue(i, 5 ,customerBean, holder);

                                break;
                        }
                        notifyDataSetChanged();

                    }
                });
            }
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) (0.16 * height));
            convertView.setLayoutParams(lp);
        }
        return convertView;
    }

    private void calculatedProgressValue(int i,  int position ,CustomerBean customerBean, ViewHolder holder) {
        current2 = (int) customerBean.getPercent();
        int total2 = 0;
        //计算除当前进度条外其他进度条的总和
        for (int j = 0; j < list.size() - 1; j++) {
            if (j != position) {
                total2 = total2 + (int) (list.get(j).getPercent());
            }
        }
        holder.customerRangeBar.setMaxSlideRange(100 - total2 );
        if (i >= 100 - total2) {
            customerBean.setPercent(100 - total2);
            holder.customerRangeBar.setProgress(100 - total2 );
        } else {
            customerBean.setPercent(i);
            holder.customerRangeBar.setProgress(i);

        }
    }

    class ViewHolder {
        TextView customer_tv;
        ImageView customer_iv;
        RangeBar customerRangeBar;

    }
}